package org.dromara.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.iot.domain.IotUpkeepProjectRecord;
import org.dromara.iot.domain.bo.IotUpkeepProjectRecordBo;
import org.dromara.iot.domain.vo.IotUpkeepProjectRecordVo;
import org.dromara.iot.mapper.IotUpkeepProjectRecordMapper;
import org.dromara.iot.service.IIotUpkeepProjectRecordService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 电梯保养项目记录Service业务层处理
 *
 * @author lsl
 * @date 2024-06-14
 */
@RequiredArgsConstructor
@Service
public class IotUpkeepProjectRecordServiceImpl implements IIotUpkeepProjectRecordService {

    private final IotUpkeepProjectRecordMapper baseMapper;

    /**
     * 查询电梯保养项目记录
     */
    @Override
    public IotUpkeepProjectRecordVo queryById(Long recordId) {
        return baseMapper.selectVoById(recordId);
    }

    /**
     * 查询电梯保养项目记录列表
     */
    @Override
    public TableDataInfo<IotUpkeepProjectRecordVo> queryPageList(IotUpkeepProjectRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IotUpkeepProjectRecord> lqw = buildQueryWrapper(bo);
        Page<IotUpkeepProjectRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询电梯保养项目记录列表
     */
    @Override
    public List<IotUpkeepProjectRecordVo> queryList(IotUpkeepProjectRecordBo bo) {
        LambdaQueryWrapper<IotUpkeepProjectRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IotUpkeepProjectRecord> buildQueryWrapper(IotUpkeepProjectRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IotUpkeepProjectRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUpkeepId() != null, IotUpkeepProjectRecord::getUpkeepId, bo.getUpkeepId());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), IotUpkeepProjectRecord::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectRequire()), IotUpkeepProjectRecord::getProjectRequire, bo.getProjectRequire());
        lqw.eq(StringUtils.isNotBlank(bo.getUpkeepResult()), IotUpkeepProjectRecord::getUpkeepResult, bo.getUpkeepResult());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectType()), IotUpkeepProjectRecord::getProjectType, bo.getProjectType());
        return lqw;
    }

    /**
     * 新增电梯保养项目记录
     */
    @Override
    public Boolean insertByBo(IotUpkeepProjectRecordBo bo) {
        IotUpkeepProjectRecord add = MapstructUtils.convert(bo, IotUpkeepProjectRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRecordId(add.getRecordId());
        }
        return flag;
    }

    /**
     * 修改电梯保养项目记录
     */
    @Override
    public Boolean updateByBo(IotUpkeepProjectRecordBo bo) {
        IotUpkeepProjectRecord update = MapstructUtils.convert(bo, IotUpkeepProjectRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotUpkeepProjectRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除电梯保养项目记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 批量新增电梯保养项目记录
     *
     * @param iotUpkeepProjectRecordBos
     */
    @Override
    public Boolean insertBatchByBo(List<IotUpkeepProjectRecordBo> iotUpkeepProjectRecordBos) {
        return baseMapper.insertBatch(MapstructUtils.convert(iotUpkeepProjectRecordBos, IotUpkeepProjectRecord.class));
    }

    /**
     * 根据保养ID查询保养项目记录
     *
     * @param upkeepId
     */
    @Override
    public List<IotUpkeepProjectRecordVo> queryListByUpkeepId(Long upkeepId) {
        LambdaQueryWrapper<IotUpkeepProjectRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(IotUpkeepProjectRecord::getUpkeepId, upkeepId);
        return baseMapper.selectVoList(lqw);
    }
}
